const app = getApp()
const {
  baseURL
} = require('../fetch/http')
const api = require('../fetch/api')
Component({
  // 组件所在页面的生命周期
  pageLifetimes: {
    
    show() {
      
      let popVisible = app.globalData.noticePopVisible;
      if (popVisible) {
        api.article({
          article_id: 6
        }).then(res => {
          this.setData({
            popupNotice: res.data.des,
            popVisible
          })
        })
      }
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
      const pages = getCurrentPages()
      const currentPage = pages[pages.length - 1]
      let id = currentPage.options.scene
      if (id) {
        wx.setStorageSync('recommend', id)
      }
    console.log(currentPage.options.id,"userid")
      if(currentPage.options.id){
        wx.setStorageSync('recommend', currentPage.options.id)
        console.log(currentPage.options.id)
      }
      wx.getStorageSync('token') && this.userinfo()
    },
  },
  lifetimes: {
    async attached() {
      this.goods()
      this.banner({
        classify:1
      })
      this.setting()
      wx.loadFontFace({
        family: "Font_Bold",
        source: 'url("https://yd.soft.haoyangsoft.com/static/wx/ttf/SM2.OTF")'
      });
    },
  },
  options: {
    // 组件样式隔离
    // "styleIsolation": "isolated"
  },
  data: {
    baseURL: baseURL,
    arr: [],
    loading: false,
    finished: false,
    page: 1,
    firstDeal: 0,
    popupNotice: "",
    popVisible: false,
    swiper:[]
  },
  methods: {
    handleNoticeConfirm() {
      console.log("我知道了");
      this.setData({
        popVisible: false
      })
      app.globalData.noticePopVisible = false;
    },
    async setting() {
      let res = await api.setting()
      let {
        ruleImg
      } = res.data
      this.setData({
        ruleImg
      })
      // console.log(res);
    },
    async banner(){
      let res = await api.banner({
        classify:1
      })
      this.setData({
        swiper:res.data
      })
    },

    async goods() {
      if (!this.data.loading) {
        this.setData({
          loading: true
        })
        let res = await api.goods({
          page: this.data.page,
          goods_type: 2
        })
        let finished = false
        res.data.length == 0 && (finished = true)
        this.setData({
          arr: [...this.data.arr, ...res.data],
          finished,
          loading: false
        })
      }
    },
    async userinfo() {
      let res = await api.userinfo()
      this.setData({
        firstDeal: res.data.firstDeal,
        id: res.data.id
      })
    },
 
    onReachBottom() {
      let {
        page,
        finished
      } = this.data
      if (!finished) {
        this.setData({
          page: page + 1
        })
        this.goods()
      }
    },
    goodsDetail(e) {
      const {
        id
      } = app.dataSet(e)
      wx.navigateTo({
        url: '/pages/home/goodsDetail/index?id=' + id
      })
    },
    onShareAppMessage: function (res) {
      if (!wx.getStorageSync('token')) {
        app.toast('请登录')
        return false
      }
      if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log(res.target)
      }
      return {
        imageUrl: this.data.baseURL + '/img/share_img_2.png',
        title: '天喜鑫宝',
        path: '/index/index?id=' + wx.getStorageSync('id')
      }
    },
    rule() {
      wx.navigateTo({
        url: '/pages/my/rule/index',
      })
    }
  }
})